package edu.bbu.commonproject.desktop.gui.controller.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * The user properties class.
 */
public class UserProperties
{
	/**	The guest user.*/
	public static final String GUESS_USER = "guess";
	
	
	/**	The user name.*/
	public static final String USERNAME = "username";
	
	/**	Synchronization active.*/
	public static final String SYNC_ACTIVE = "syncactive";
	
	/**	The synchronization type.*/
	public static final String SYNC_TYPE = "synctype";
	
	/**	The synchronization local path.*/
	public static final String SYNC_LOCAL_PATH = "synclocalpath";
	
	/**	The synchronization local directory.*/
	public static final String SYNC_LOCAL_DIRECTORY = "synclocaldirectory";
	
	
	/**	The user name.*/
	private static String userName;
	
	/**	The properties file patch.*/
	private static String propFilePatch;
	
	/**	The properties.*/
	private static Properties properties;
	
	/**
	 * The constructor.
	 */
	public UserProperties()
	{
		this(GUESS_USER);
	}
	
	/**
	 * The constructor.
	 * @param userName the user name.
	 */
	public UserProperties(String userName)
	{
		this.userName = userName;
		this.properties = new Properties();
		this.propFilePatch = "conf" + File.separator + userName + ".prop";
		
		File file = new File(propFilePatch);
		
		if(file.exists())
		{
			try
			{
				FileInputStream inputStream = new FileInputStream(file);
				properties.load(inputStream);
				properties.setProperty(USERNAME, userName);
				inputStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		else 
		{
			createDefault();
			
			try
			{
				FileOutputStream outputStream = new FileOutputStream(file);
				properties.store(outputStream, null);
				outputStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Creating the default user.
	 */
	public synchronized void createDefault()
	{
		properties.setProperty(USERNAME, userName);
		properties.setProperty(SYNC_ACTIVE, "false");
		properties.setProperty(SYNC_TYPE, "2");
		properties.setProperty(SYNC_LOCAL_PATH, System.getProperty("user.dir") + File.separator);
		properties.setProperty(SYNC_LOCAL_DIRECTORY, "sync");
	}
	
	/**
	 * Getting the value of a property of the user.
	 * @param key the property key.
	 * @return the user property.
	 */
	public synchronized String getValue(String key)
	{
		return properties.getProperty(key, "");
	}
	
	/**
	 * Setting the value of a property of the user.
	 * @param key the property key.
	 * @param value the value of a property.
	 */
	public synchronized void setValue(String key, String value)
	{
		properties.setProperty(key, value);
	}
	
	/**
	 * Save a property.
	 */
	public synchronized void save()
	{
		if(userName != null)
		{
			File file = new File(propFilePatch);
			
			try
			{
				FileOutputStream outputStream = new FileOutputStream(file);
				properties.store(outputStream, null);
				outputStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
}
